package com.example.newdemo.tv;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.newdemo.BaseActivity;
import com.example.newdemo.BaseFragment;
import com.example.newdemo.R;
import com.example.newdemo.adapter.BigImageAdapter;
import com.example.newdemo.bean.ImageBean;
import com.example.newdemo.utils.LogUtils;
import com.example.newdemo.view.recyclerView.demo1.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class ImageRecyclerFragment extends BaseFragment {


    private LinearLayoutManager linearLayoutManager;
    private List<ImageBean> imageList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_image_recycler;
    }

    @Override
    protected void initView() {
        RecyclerViewTV recyclerView =  rootView.findViewById(R.id.recycler_big_image_view);

        linearLayoutManager = new LinearLayoutManager(getActivity());

        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        imageList = getImageList();
        BigImageAdapter adapter = new BigImageAdapter(getActivity(), imageList);
        recyclerView.setAdapter(adapter);

        DividerItemDecoration decoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);

        recyclerView.addItemDecoration(decoration);

//        recyclerView.addOnScrollListener(recyclerScrollListener);



        recyclerView.setOnItemListener(new RecyclerViewTV.OnItemListener() {
            @Override
            public void onItemPreSelected(RecyclerViewTV parent, View itemView, int position) {
                LogUtils.d(" 失去焦点 ** onItemPreSelected   postion = " + position);
            }

            @Override
            public void onItemSelected(RecyclerViewTV parent, View itemView, int position) {
                LogUtils.d(" 获得  -- onItemSelected   postion = " + position);
                showLongToast("position = " + position);
            }

            @Override
            public void onReviseFocusFollow(RecyclerViewTV parent, View itemView, int position) {
                LogUtils.d("修改  /// onReviseFocusFollow   postion = " + position);

            }
        });

        recyclerView.setOnItemClickListener(new RecyclerViewTV.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerViewTV parent, View itemView, int position) {
                LogUtils.d("oneLeverecyclerView  --点击----- position = " + position);


            }
        });



    }

    @Override
    protected void initData() {

    }

    private List<ImageBean> getImageList(){
        ArrayList<ImageBean> imageList = new ArrayList<>();
        imageList.add(new ImageBean(R.drawable.s1));
        imageList.add(new ImageBean(R.drawable.s2));
        imageList.add(new ImageBean(R.drawable.s3));
        imageList.add(new ImageBean(R.drawable.s4));
        imageList.add(new ImageBean(R.drawable.s5));
        imageList.add(new ImageBean(R.drawable.s6));
        imageList.add(new ImageBean(R.drawable.s7));
        imageList.add(new ImageBean(R.drawable.s8));
        imageList.add(new ImageBean(R.drawable.s9));

        return imageList;
    }



    RecyclerView.OnScrollListener recyclerScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {

            if (RecyclerView.SCROLL_STATE_IDLE == newState){    // 停止滑动
                int firstVisiblePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                int lastVisiblePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();

                LogUtils.d("firstVisiblePosition = " +firstVisiblePosition +
                        "; firstVisibleItemPosition = " + firstVisibleItemPosition +
                        " ; lastVisiblePosition = " + lastVisiblePosition +
                        " ; lastVisibleItemPosition = " + lastVisibleItemPosition );

                if (firstVisibleItemPosition == 0 && lastVisibleItemPosition == 0){
                    showShortToast(" -- 顶部 --");
                }

                int maxSize = imageList.size() - 1;
                if (lastVisiblePosition == maxSize && firstVisibleItemPosition == maxSize){
                    showShortToast(" ** bottom ** ");
                }

            }



            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }
    };
}
